#!/bin/bash

##############################################################################
#
# Setup a build environment. This can take up to two parameters:
# argv[1] - build type, can be 'stable' or 'master'; anything else than
#           'stable' means 'master' build in the ./Makefile, which uses
#           relative path for build and if the respective folder is not
#           downloaded yet, then it creates a checkout from the master git
#           branch (hence the 'master' build name). There can be built
#           anything as master, the only condition is that the directory
#           with the sources is in place.
# argv[2] - use "debug" to build with debug options; using 'debug' build type
#           also uses debug build options
#
##############################################################################

if [ "$BUILD_ROOT_OVERRIDE" = "" ] ; then
	export BUILD_ROOT_BASE=/build/local
else
	export BUILD_ROOT_BASE=$BUILD_ROOT_OVERRIDE
fi

# Optional parameter to compile other than 'master' or 'stable' releases;
# empty value means 'stable', anything else means 'master' type in Makefile
export EVO_BUILD_TYPE=$1

if [ "x$EVO_BUILD_TYPE" == "x" ] ; then
	# Build 'master' as default
	export EVO_BUILD_SUFFIX="-master"
	export EVO_BUILD_TYPE="master"
elif [ "x$EVO_BUILD_TYPE" != "xstable" ] ; then
	# Build custom builds like from 'master', aka from relative path checkouts
	export EVO_BUILD_SUFFIX="-master"
else
	# Build from tarballs
	export EVO_BUILD_SUFFIX="-stable"
	export EVO_BUILD_TYPE="stable"
fi

export BUILD_ROOT_EVO=$BUILD_ROOT_BASE/$EVO_BUILD_TYPE
export BUILD_ROOT_DEPS=$BUILD_ROOT_EVO

if [ "$PATH_DEFAULT" = "" ] ; then
	export PATH_DEFAULT=$PATH
fi

export PATH=$BUILD_ROOT_DEPS/bin:$BUILD_ROOT_EVO/bin:$PATH_DEFAULT

# Compile against locally installed software.
export LD_LIBRARY_PATH=$BUILD_ROOT_DEPS/lib:$BUILD_ROOT_EVO/lib:/lib
export PKG_CONFIG_PATH=$BUILD_ROOT_DEPS/lib/pkgconfig:$BUILD_ROOT_EVO/lib/pkgconfig:$BUILD_ROOT_DEPS/share/pkgconfig:$BUILD_ROOT_EVO/share/pkgconfig
export ACLOCAL_FLAGS="-I $BUILD_ROOT_DEPS/share/aclocal -I $BUILD_ROOT_EVO/share/aclocal -I /share/aclocal"
export GSETTINGS_SCHEMA_DIR=$BUILD_ROOT_EVO/share/glib-2.0/schemas
export CPPFLAGS="$CPPFLAGS -I$BUILD_ROOT_DEPS/include -I$BUILD_ROOT_EVO/include -I/include"
export LDFLAGS="$LDFLAGS -L$BUILD_ROOT_DEPS/lib -L$BUILD_ROOT_EVO/lib -L/lib"
if [ "$1" = "debug" -o "$2" = "debug" ] ; then
	export CFLAGS="$CFLAGS -g -O0 -Wall -DMSVCRT_VERSION=710"
else
	export CFLAGS="$CFLAGS -g0 -O2 -DMSVCRT_VERSION=710"
	export LDFLAGS="$LDFLAGS -Wl,-S"
fi

mkdir -p $BUILD_ROOT_DEPS/bin 2>/dev/null
mkdir -p $BUILD_ROOT_DEPS/include 2>/dev/null
mkdir -p $BUILD_ROOT_DEPS/lib 2>/dev/null
mkdir -p $BUILD_ROOT_DEPS/libexec 2>/dev/null
mkdir -p $BUILD_ROOT_DEPS/share/aclocal 2>/dev/null
mkdir -p $BUILD_ROOT_DEPS/etc/dbus-1/session.d 2>/dev/null
#mkdir -p $BUILD_ROOT_DEPS/share/dbus-1 2>/dev/null

mkdir -p $BUILD_ROOT_EVO/bin 2>/dev/null
mkdir -p $BUILD_ROOT_EVO/include 2>/dev/null
mkdir -p $BUILD_ROOT_EVO/lib 2>/dev/null
mkdir -p $BUILD_ROOT_EVO/libexec 2>/dev/null
mkdir -p $BUILD_ROOT_EVO/share/aclocal 2>/dev/null
#mkdir -p $BUILD_ROOT_EVO/share/dbus-1 2>/dev/null

#rm /usr/local/share/dbus-1 2>/dev/null
#ln -s $BUILD_ROOT_DEPS/share/dbus-1 /usr/local/share/dbus-1

if [ -f "session-local.conf.in" ] ; then
	cd $BUILD_ROOT_EVO
	WLIKE_PWD=`pwd -W | sed 's|/|\\\\\\\\|g'`
	cd - >/dev/null
	cat session-local.conf.in | sed 's|@BUILD_ROOT_EVO@|'$WLIKE_PWD'\\share\\dbus-1\\services|g' >$BUILD_ROOT_DEPS/etc/dbus-1/session-local.conf
fi
